package com.useeinfo.oa.open.api.automaticRepair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.EnterpriseBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.repair.biz.AutomaticRepairBiz;
import com.useeinfo.oa.modules.repair.biz.RepairLevelBiz;
import com.useeinfo.oa.modules.repair.biz.RepairOrderBiz;
import com.useeinfo.oa.modules.repair.biz.TroubleCatBiz;
import com.useeinfo.oa.modules.repair.entity.RepairOrder;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/automaticRepair")
public class AutomaticRepairAPI {


    @Autowired
    private HttpServletRequest request;
    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @Autowired
    private AutomaticRepairBiz automaticRepairBiz;
    @Autowired
    private TroubleCatBiz troubleCatBiz;
    @Autowired
    private RepairLevelBiz repairLevelBiz;
    @Autowired
    private RepairOrderBiz repairOrderBiz;

    /**
     * 报修单列表接口
     * 1:待接单
     * 2:未完成
     * 3:审批中
     * 4:已结束
     */
    @RequestMapping("/automaticRepairList")
    @ResponseBody
    public ResultDto automaticRepairList(String pageIndex, String pageSize, String status, String searchParam, String date) {
        SystemUser systemUser = SessionKeyUtils.getSessionUser(request);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (null == systemUser) {
            return ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }
        if (null == enterprise) {
            return ResultUtil.error("企业信息有误");
        }
        return automaticRepairBiz.getAutomaticRepair(pageIndex, pageSize, status, searchParam, systemUser, enterprise, date);
    }

    /**
     * 报修单详情
     */
    @RequestMapping("/automaticRepairDetail")
    @ResponseBody
    public ResultDto automaticRepairDetail(String repairOrderId) {
        return automaticRepairBiz.automaticDetail(repairOrderId);
    }

    /**
     * 关注
     */
    @RequestMapping("/focus")
    @ResponseBody
    public ResultDto focus(String repairOrderId, Boolean type) {
        return automaticRepairBiz.focus(repairOrderId, type);
    }

    /**
     * 添加/修改工作日志
     */
    @RequestMapping("/addLog")
    @ResponseBody
    public ResultDto addLog(String logId, String workLogCatId, String workLogLevelId, String workContent, String photos, String repairOrderId) {
        return automaticRepairBiz.addLog(logId, workLogCatId, workLogLevelId, workContent, photos, repairOrderId);
    }

    /**
     * 工作日志列表
     */
    @RequestMapping("/repairWorkLogList")
    @ResponseBody
    public ResultDto repairWorkLogList(String pageIndex, String pageSize, String id) {
        return automaticRepairBiz.repairWorkLogList(pageIndex, pageSize, id);
    }

    /**
     * 工作日志详情
     */
    @RequestMapping("/repairWorkLogDetail")
    @ResponseBody
    public ResultDto repairWorkLogDetail(String id) {
        return automaticRepairBiz.repairWorkLogDetail(id);
    }

    /**
     * 接单接口
     */
    @RequestMapping("/taking")
    @ResponseBody
    public ResultDto taking(String repairOrderId) {
        return automaticRepairBiz.taking(repairOrderId);
    }

    /**
     * 到场
     */
    @RequestMapping("/presence")
    @ResponseBody
    public ResultDto presence(String presencePhotos, String repairOrderId, String longitude, String latitude) {
        return automaticRepairBiz.presence(presencePhotos, repairOrderId, longitude, latitude);
    }

    /**
     * 上传接口
     */
    @RequestMapping("/upload")
    @ResponseBody
    public ResultDto upload(MultipartFile file, String type) {
        return automaticRepairBiz.upload(file, type);
    }

    /**
     * 临时保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultDto save(String repairOrderId, String spareParts, String finishPhotos, String unrepairReason, String measure, String faultReason, Boolean inRepairScope, String troubleCatId, String workingHours) {
        return automaticRepairBiz.save(repairOrderId, spareParts, finishPhotos, unrepairReason, measure, faultReason, inRepairScope, troubleCatId, workingHours);
    }

    /**
     * 保存指定人
     */
    @RequestMapping("/saveAssignList")
    @ResponseBody
    public ResultDto saveAssignList(String assignIds, String repairOrderId) {
        return automaticRepairBiz.assignUsers(assignIds, repairOrderId);
    }

    /**
     * 保存指定岗位
     */
    @RequestMapping("/saveAssignPositionList")
    @ResponseBody
    public ResultDto saveAssignPositionList(String assignPositionIds, String repairOrderId) {
        return automaticRepairBiz.assignPositions(assignPositionIds, repairOrderId);
    }

    /**
     * 审核
     */
    @RequestMapping("/audit")
    @ResponseBody
    public ResultDto audit(String repairOrderId, Boolean type, String remark) {
        return automaticRepairBiz.audit(repairOrderId, type, remark);
    }

    /**
     * 巡检维修到场
     */
    @RequestMapping("/inspectionPresence")
    @ResponseBody
    public ResultDto inspectionPresence(String presencePhotos, String repairOrderId, String longitude, String latitude) {
        return automaticRepairBiz.inspectionPresence(presencePhotos, repairOrderId, longitude, latitude);
    }

    /**
     * 区域列表
     */
    @RequestMapping("/areaList")
    @ResponseBody
    public ResultDto areaList(String pageIndex, String pageSize, String searchParam) {
        return automaticRepairBiz.areaList(pageIndex, pageSize, searchParam);
    }

    /**
     * 故障分类
     */
    @RequestMapping("/troubleCatList")
    @ResponseBody
    public ResultDto troubleCatList() {
        return troubleCatBiz.troubleCatList();
    }

    /**
     * 字典下拉选接口
     */
    @RequestMapping("/getDictSelect")
    @ResponseBody
    public ResultDto getDictSelect(String typeCode) {
        return automaticRepairBiz.getDictSelect(typeCode);
    }

    /**
     * 根据单号获取维修单信息
     */
    @RequestMapping("/getOrdersByCode")
    @ResponseBody
    public ResultDto getOrdersByCode(String repairCode) {
        return automaticRepairBiz.getOrdersByCode(repairCode);
    }

    /**
     * 流程接口
     *
     * @param repairOrderId 报修单id
     * @return
     */
    @RequestMapping("/process")
    @ResponseBody
    public ResultDto process(String repairOrderId) {
        return automaticRepairBiz.process(repairOrderId);
    }

    /**
     * 添加参与人
     */
    @RequestMapping("/addPartner")
    @ResponseBody
    public ResultDto addPartner(String repairOrderId, String partnerArray) {
        return automaticRepairBiz.addPartner(repairOrderId, partnerArray);
    }

    /**
     * 移除参与人
     */
    @RequestMapping("/removePartner")
    @ResponseBody
    public ResultDto removePartner(String id) {
        return automaticRepairBiz.removePartner(id);
    }

    /**
     * 紧急程度
     */
    @RequestMapping("/repairLevel")
    @ResponseBody
    public ResultDto repairLevel() {
        return repairLevelBiz.repairLevel();
    }

    /**
     * 终审评分
     */
    @RequestMapping("/finalAudit")
    @ResponseBody
    public ResultDto finalAudit(String repairOrderId, String score, String remark) {
        return automaticRepairBiz.finalAudit(repairOrderId, remark, score);
    }

    /**
     * 确认故障
     */
    @RequestMapping("/confirmFault")
    @ResponseBody
    public ResultDto confirmFault(String repairOrderId, String troubleCatId, String faultReason, Boolean inRepairScope) {
        return automaticRepairBiz.confirmFault(repairOrderId, troubleCatId, faultReason, inRepairScope);
    }

    /**
     * 维修-确认完成
     */
    @RequestMapping("/repairConfirm")
    @ResponseBody
    public ResultDto repairConfirm(String repairOrderId, String participantInputs, String spareParts, String finishPhoto, String unrepairReason, String measure, String repairFinishTime, String faultReason, Boolean inRepairScope, String troubleCatId) {
        RepairOrder repairOrder = repairOrderBiz.findModel(StringConverters.ToLong(repairOrderId));
        JSONArray jsonArray = JSON.parseArray(participantInputs);
        return automaticRepairBiz.repairConfirm(repairOrder, jsonArray, spareParts, finishPhoto, unrepairReason, measure, repairFinishTime, faultReason, inRepairScope, troubleCatId);
    }

    /**
     * 维修历史纪录
     */
    @RequestMapping("/repairHistory")
    @ResponseBody
    public ResultDto repairHistory(String facilityId, String pageIndex, String pageSize) {
        return automaticRepairBiz.repairHistory(facilityId, pageIndex, pageSize);
    }

    /**
     * 转单
     */
    @RequestMapping("/transfer")
    @ResponseBody
    public ResultDto transfer(String repairOrderId, String userIds) {
        return automaticRepairBiz.transfer(repairOrderId, userIds);
    }

    /**
     * 转单列表
     */
    @RequestMapping("/transferList")
    @ResponseBody
    public ResultDto transferList(String searchParam, String pageIndex, String pageSize) {
        return automaticRepairBiz.transferList(searchParam, pageIndex, pageSize);
    }

    /**
     * 指派人员列表
     */
    @RequestMapping("/getUser")
    @ResponseBody
    public ResultDto getUser(String pageIndex, String pageSize, String userName, String userIds) {
        return automaticRepairBiz.getUser(pageIndex,pageSize,userName,userIds);
    }

    /**
     * 指派岗位列表
     */
    @RequestMapping("/getPosition")
    @ResponseBody
    public ResultDto getPosition(String pageIndex, String pageSize,String positionIds) {
        return  automaticRepairBiz.getPosition(pageIndex,pageSize,positionIds);
    }

    /**
     * 参与人详情
     */
    @RequestMapping("/partnerDetail")
    @ResponseBody
    public ResultDto partnerDetail(String repairOrderId,String userId){
        return automaticRepairBiz.partnerDetail(repairOrderId,userId);
    }
}
